﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Procure.Data.Context
{
    public static class DataFactory
    {
        public static ProcureDataContext GetDataContext()
        {
            // if running inside of umbraco, use the umbraco connection string
            string connectionString = string.Empty;
            // first try to get a connection string for Umbraco from the web.config
            if (!string.IsNullOrEmpty(ConfigurationSettings.AppSettings["umbracoDbDSN"]))
                connectionString = ConfigurationSettings.AppSettings["umbracoDbDSN"];
            // if that fails, try to get one specifically for the data context from the ConnectionStrings section
            else if (ConfigurationManager.ConnectionStrings["Procure.Data.Properties.Settings.ProcureConnectionString"] != null)
                connectionString = ConfigurationManager.ConnectionStrings["Procure.Data.Properties.Settings.ProcureConnectionString"].ConnectionString;
            // otherwise, throw an exception indicating that a connection string could not be found.
            else
                throw new NotImplementedException("No connection string was found in UmbracoDSN or ConnectionStrings section.");
            
            // new up a data context with the determined connection string
            ProcureDataContext dataContext = new ProcureDataContext(connectionString);
            
            // return the created data context
            return dataContext;
        }
    }
}
